home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / feel0_89.lha / Feel / AddOns / euclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-13  |  27.8 KB  |  1,058 lines

  1.  
  2. /* 
  3.   Project: YY-X
  4.   File: Euclient.c
  5.   Created: 19/9/90
  6.   ANSIfied: 1/10/90
  7. */
  8.  
  9. /* ***
  10.   Eulisp 'C' module interface to YY-X
  11.  
  12.   Current implementation handles commands and 
  13.   instructions only
  14.  
  15.   Functions:
  16.     Initialise(port-no,server)  connects with the server
  17.     YY-Function(cmd_num,args)   Executes commands
  18.     YY-Function-Info(cmd_num)   Cheap help system  -- I'll write it one day
  19.  
  20.   PROBLEMS:
  21.     Malloc abuse
  22.   *** */
  23.  
  24. #ifdef WITH_SYSTEMV_SOCKETS
  25. #include <poll.h>
  26. #endif
  27. #ifdef notdef /* Stream ioctls */
  28. #include <stropts.h>
  29. #endif
  30. #include <stdio.h>
  31. #include <fcntl.h>
  32. #include <signal.h>
  33. #include <sys/types.h> 
  34. #ifdef hlh
  35. #include "orion-types.h"
  36. #endif
  37.  
  38.  
  39. /* #include <sys/resource.h>*/
  40. /* #include <sys/socket.h> Included elsewhere*/
  41. #include <sys/un.h>
  42. /* #include <netinet/in.h> */
  43. /* #include <netdb.h> Included elsewhere */
  44. #include <strings.h>
  45.  
  46. /* Eulisp Stuff */
  47. #include "defs.h"
  48. #include "funcalls.h"
  49. #include "structs.h"
  50. #include "global.h"
  51. #include "error.h"
  52. #include "bootstrap.h"
  53.  
  54. /* YY stuff */
  55. #include "yydefs.h"
  56. #include "yypacket.h"
  57. #include "eucmd.h"
  58.  
  59. /* Magic number */
  60. #define YYMAGIC                         14876
  61.  
  62. #define CLIENTTIMEOUT_INUSEC            2000
  63. /* incoming buffer size */
  64. #define MAX_REPLY_STRING                 200
  65.  
  66. #define YYHEADERSIZE   sizeof(yy_packet_header)
  67.  
  68. /* Some Global Values */
  69. static SYSTEM_GLOBAL(int,packet_size);
  70.  
  71. /* This should be safe... */
  72. /* assuming I set it each time I enter the routine */
  73. int YYPacketBlockSize;
  74.  
  75.     /* File descriptors */
  76. static SYSTEM_GLOBAL(int,fd1);
  77. static SYSTEM_GLOBAL(int,fd2);    
  78.  
  79. /* MAIN PROTOCOL FUNCTION */
  80.  
  81. extern command_entry ClientCmdTable[];
  82.  
  83. /* lispy stuff */
  84. #define ALLOCATE_PAIR(car,cdr)   EUCALL_2(Fn_cons,(car),(cdr))
  85.  
  86.  
  87. EUFUN_2(Fn_YY_Function, cmd_num, arg_list)
  88. {
  89.   LispObject packet2list(LispObject *stacktop,yy_packet **p_pkt, reply_entry *re);
  90.   int read_reply2q(int sock,int cmd_num, yy_packet_system_queue *p_que,
  91.            yy_packet **p_pkt);
  92.   void make_packet(LispObject *,int cmd,LispObject alist,yy_packet **p_pkt);
  93.   int send_packet(int sock, yy_packet *pkt);
  94.  
  95.   yy_packet_system_queue que;
  96.   yy_packet *pkt;
  97.   reply_entry *re;
  98.  
  99.   /* Needed for returning stuff */
  100.   LispObject ret_list;
  101.   
  102.   /* Stack ... */
  103.  
  104.   /* Set a global value */
  105.   YYPacketBlockSize = SYSTEM_GLOBAL_VALUE(packet_size);
  106.  
  107.   if (!is_fixnum(cmd_num))
  108.     CallError(stacktop,"YY-Funcion: Was expecting an integer command number", nil, NONCONTINUABLE);
  109.       
  110.  
  111.   make_packet(stacktop,intval(cmd_num),arg_list,&pkt);
  112.  
  113.   fix_yy_packet(pkt);
  114.  
  115.   if (!send_packet(SYSTEM_GLOBAL_VALUE(fd1),pkt))
  116.     CallError(stacktop, "sending error", nil,NONCONTINUABLE);
  117.  
  118.   /* Free packet structure (!) */
  119.   remove_yy_packet(pkt);
  120.   
  121.   /* Handle incoming junk */
  122.   /* And return it */
  123.  
  124.   re = ClientCmdTable[intval(cmd_num)].ceReplyData;
  125.   if (re!=NULL)
  126.     {
  127.       ret_list = nil;
  128.       read_reply2q(SYSTEM_GLOBAL_VALUE(fd1),intval(cmd_num),&que,&pkt);
  129.  
  130.       ret_list=packet2list(stacktop,&pkt,re);
  131.       return ret_list;
  132.     }
  133.   else
  134.     {
  135.       return nil;
  136.     }
  137. }
  138. EUFUN_CLOSE
  139.  
  140. int read_reply2q(int sock,int cmd_num,
  141.          yy_packet_system_queue *p_que,
  142.          yy_packet **p_pkt)
  143. {
  144.   do
  145.     {
  146.       recv_packet(sock,p_que);
  147.       *p_pkt= (yy_packet *) get_packet_from_readq(p_que);
  148.     } while ( (*p_pkt)->pktCommand != cmd_num);
  149. }
  150.  
  151. int read_event2q(int sock,
  152.          yy_packet_system_queue *p_que,
  153.          yy_packet **p_pkt,
  154.          int block)
  155. {
  156.   int poll_eventq(int fd);
  157.  
  158.   if (!block || poll_eventq(sock))
  159.     {
  160.       recv_packet(sock,p_que);
  161.       *p_pkt = (yy_packet *) get_packet_from_readq(p_que);
  162.       return TRUE;
  163.     }
  164.   else
  165.     return FALSE;
  166. }
  167.  
  168. /*
  169.   * Event Handling function 
  170.   */
  171.  
  172. EUFUN_1( Fn_get_next_event, block)
  173. {     
  174.   LispObject packet2list(LispObject *stacktop,yy_packet **p_pkt, reply_entry *re);
  175.   LispObject ret_list=nil;
  176.   yy_packet_system_queue que;
  177.   yy_packet *pkt;
  178.   reply_entry *re;
  179.  
  180.   if (read_event2q(SYSTEM_GLOBAL_VALUE(fd2),&que,&pkt,(block==nil?TRUE:FALSE)))
  181.     {    
  182.       int rval;
  183.  
  184.       re = ClientCmdTable[pkt->pktCommand].ceReplyData;
  185.       ret_list=packet2list(stacktop,&pkt,re);
  186.       return ret_list;
  187.     }
  188.   else 
  189.     {
  190.       return nil;
  191.     }
  192. }
  193. EUFUN_CLOSE
  194.  
  195. LispObject packet2list(LispObject *stacktop,yy_packet **p_pkt, reply_entry *re)
  196. {
  197.   LispObject read_font_data(LispObject *stacktop, yy_packet *packet);
  198.   LispObject ret=nil;
  199.   LispObject end;
  200.  
  201.   char buf[MAX_REPLY_STRING];
  202.   int len;
  203.  
  204.   if (re->reType== -1)
  205.     return ret;
  206.   else
  207.     {
  208.       while (re->reType!= -1)
  209.     {
  210.       if (ret==nil)
  211.         {
  212.           ret = ALLOCATE_PAIR(nil,nil);
  213.           end = ret;
  214.         }
  215.       else 
  216.         {
  217.           CDR(end)=ALLOCATE_PAIR(nil,nil);
  218.           end=CDR(end);
  219.         }
  220.       switch(re->reType)
  221.         {
  222.         case ARG_STR:
  223.           len = read_packet_entry_integer(*p_pkt);
  224.           read_packet_entry_string(*p_pkt,len,buf);
  225.           CAR(end)=allocate_string(stacktop,buf,len);
  226.           break;
  227.           
  228.         case ARG_INT:
  229.           CAR(end) = real_allocate_integer(stacktop,read_packet_entry_integer(*p_pkt));
  230.           break;
  231.           
  232.         case ARG_FONTDATA:
  233.           CAR(end)=read_font_data(stacktop,*p_pkt);
  234.           break;
  235.  
  236.         default:
  237.           fprintf(stderr,"Error in YY reply data\n");
  238.           break;
  239.         }
  240.       re ++;
  241.     }    
  242.     }
  243.   remove_yy_packet(*p_pkt);
  244.   return ret;
  245. }
  246.  
  247. void make_packet(LispObject *stacktop,int cmd,LispObject alist,yy_packet **p_pkt)
  248. {
  249.   yy_packet *pkt;
  250.   cmd_arg *ce;
  251.   LispObject point_list;
  252.  
  253.   pkt = alloc_new_yy_packet(cmd,YYPACKETTYPE_COMMAND,0,NULL,0);
  254.   
  255.   if (ClientCmdTable[cmd].cmdArgs==NULL)
  256.     CallError(stacktop,"YY-function: Unimplemented Operation", nil, NONCONTINUABLE);    
  257.  
  258.   ce = ClientCmdTable[cmd].cmdArgs;
  259.  
  260.   /* Send the data */
  261.   while(ce->argType!= -1)
  262.     {
  263.       if (null(alist))
  264.     {
  265.       CallError(stacktop,"Not enough arguments to YY-function",nil,NONCONTINUABLE);        
  266.     }    
  267.  
  268.       switch(ce->argType)
  269.     {
  270.     case ARG_INT:
  271.       if (!is_fixnum(CAR(alist)))
  272.         CallError(stacktop,"YY-function: Expecting an integer", nil, NONCONTINUABLE);
  273.       
  274.       append_packet_entry_integer(pkt,intval((CAR(alist))));
  275.       break;
  276.       
  277.     case ARG_STR:
  278.       if (!is_string(CAR(alist)))
  279.         CallError(stacktop,"YY-Function: YY-Function: Expecting a string",
  280.               CAR(alist),
  281.               NONCONTINUABLE);
  282.  
  283.       append_packet_entry_string(pkt,stringof(CAR(alist)));
  284.       break;
  285.  
  286.       /* Want a list with even number of elements */
  287.     case ARG_PAIR_LIST:
  288.       point_list = CAR(alist);
  289.       if (null(point_list))
  290.         CallError(stacktop,"YY-Function: Lists must be non-empty",point_list,NONCONTINUABLE);
  291.  
  292.           while (!null(point_list))
  293.         {
  294.           if (!is_cons(point_list))
  295.             CallError(stacktop,"YY-Function: YY-Function: Expecting a list",
  296.                   point_list, NONCONTINUABLE);
  297.       
  298.           if (!is_cons(CAR(point_list)))
  299.             CallError(stacktop,"YY-function: Need list-of coord pairs",
  300.                   CAR(point_list), NONCONTINUABLE);
  301.  
  302.           if (!is_fixnum(CAR(CAR(point_list))))
  303.             CallError(stacktop,"YY-function: Expecting an integer [1]", CAR(point_list),
  304.                   NONCONTINUABLE);
  305.  
  306.           if (!is_fixnum(CDR(CAR(point_list))))
  307.             CallError(stacktop,"YY-function: Expecting an integer [2]",
  308.                   (CDR(CAR(point_list))),
  309.                   NONCONTINUABLE);
  310.           
  311.           append_packet_entry_integer(pkt,intval(CAR(CAR(point_list))));
  312.           append_packet_entry_integer(pkt,intval(CDR(CAR(point_list))));
  313.           
  314.           point_list = (CDR(point_list));
  315.         }
  316.       break;
  317.  
  318.     default:
  319.       fprintf(stderr,"Error in YY init data\n");
  320.       break;
  321.     }
  322.       ce ++;
  323.       alist = CDR(alist);
  324.     }
  325.   
  326.   if(!null(alist))
  327.     {    
  328.       CallError(stacktop,"Too many arguments to YY-Function",alist,NONCONTINUABLE);
  329.     }
  330.   
  331.   *p_pkt = pkt;
  332.  
  333. }
  334. /* Reading the font data */
  335. /*  ****
  336. * From:   (according to the documentation)
  337. *  font_char $B9=B$BN(B for each ascii character 
  338. *   struct font_char {
  339. *     char code;        charcter code
  340. *     char width;       width     
  341. *     char height;      height    
  342. *     char base;        base-height    
  343. *   };
  344. *
  345. *    font $B9=B$BN(B for a font set
  346. *  struct font {
  347. *     int width;                           fixed width for kanji 
  348. *     int height;                          fixed height for kanji  
  349. *     int base;                            fixed base-height for kanji 
  350. *     struct font_char code_char[256];     256 font_char structs 
  351. *   };
  352. *
  353. *  into: 
  354. *    (width height base (code width height base) ...)
  355. *   *** */
  356. LispObject read_font_data(LispObject *stacktop,yy_packet *packet,LispObject *l_ptr)
  357. {
  358.   LispObject res;
  359.   LispObject font_ptr;
  360.   LispObject char_ptr;
  361.   int i;
  362.  
  363.   res = ALLOCATE_PAIR(nil,nil);
  364.   font_ptr=res;
  365.   /* width */
  366.   CAR(font_ptr)=real_allocate_integer(stacktop,read_packet_entry_integer(packet));
  367.   /* height */
  368.   CDR(font_ptr) = ALLOCATE_PAIR(real_allocate_integer(stacktop,read_packet_entry_integer(packet)),nil);
  369.   font_ptr = CDR(font_ptr);
  370.   /* base */
  371.   CDR(font_ptr) = ALLOCATE_PAIR(real_allocate_integer(stacktop,read_packet_entry_integer(packet)),nil);
  372.   font_ptr = CDR(font_ptr);
  373.  
  374.   for (i=0; i<256 ; i++)
  375.     {
  376.       CDR(font_ptr)=ALLOCATE_PAIR(nil,nil);
  377.       font_ptr = CDR(font_ptr);
  378.       
  379.       char_ptr=  ALLOCATE_PAIR(real_allocate_integer(stacktop,read_packet_entry_onebyte(packet)),nil);
  380.       CAR(font_ptr)=char_ptr;
  381.  
  382.       /* width */
  383.       CDR(char_ptr) =  ALLOCATE_PAIR(real_allocate_integer(stacktop,read_packet_entry_onebyte(packet)),nil);
  384.       char_ptr = CDR(char_ptr);
  385.       /* Height */
  386.       CDR(char_ptr)=  ALLOCATE_PAIR(real_allocate_integer(stacktop,read_packet_entry_onebyte(packet)),nil);
  387.       char_ptr = CDR(char_ptr);
  388.       /* Base */
  389.       CDR(char_ptr) =  ALLOCATE_PAIR(real_allocate_integer(stacktop,read_packet_entry_onebyte(packet)),nil);
  390.       char_ptr = CDR(char_ptr);
  391.     }
  392.   return res;
  393. }
  394.  
  395.     
  396.       
  397. int poll_eventq(int fd)
  398. {
  399. #ifdef WITH_SYSTEMV_SOCKETS  
  400.   {
  401.     struct pollfd rfds[1];
  402.  
  403.     int timeout;
  404.     int ret;
  405.   
  406.     rfds[0].fd= fd;
  407.     rfds[0].events= POLLIN;
  408.     rfds[0].revents= 0;
  409.  
  410.     timeout = 10;
  411.     if ((ret = poll(rfds, 1, timeout)) < 0) 
  412.       {
  413.     perror("poll(event)");
  414.       }
  415.  
  416.     if (rfds[0].revents & POLLIN)
  417.       return TRUE;
  418.     else    
  419.       return FALSE;
  420.   }
  421. #endif
  422. #ifdef WITH_BSD_SOCKETS
  423.   {
  424.     fd_set readfds;
  425.     struct timeval timeout;
  426.     int ret;
  427.  
  428.     timeout.tv_sec = 0;
  429.     timeout.tv_usec = 10;
  430.  
  431.     FD_ZERO(&readfds);
  432.     FD_SET(fd,&readfds);
  433.     if ( (ret =select (fd, &readfds, NULL, NULL, timeout)) == 1)
  434.       return TRUE;
  435.     else 
  436.       {
  437.     if (ret == -1)
  438.       {
  439.         perror("Poll_fd:Select");
  440.         exit(1);
  441.       }
  442.     else 
  443.       return FALSE;
  444.       }
  445.   }
  446. #endif  
  447. }
  448.  
  449. /* STARTUP FUNCTIONS */
  450. EUFUN_2( Fn_setup_server, hostname, dnumber)
  451. {
  452.     int data, nwords;
  453.     int idnumber;
  454.  
  455.     idnumber= intval(dnumber);
  456.     
  457.     debug_init(".yydebug",stderr,NULL,NULL);
  458.  
  459.     /* $B%A%'%C%/(B $B%M%C%H%o!<%/(B */
  460.     SYSTEM_GLOBAL_VALUE(fd1) = get_inet_domain(stringof(hostname), idnumber);
  461.     
  462.     data = YYMAGIC;
  463.     /* $B%^%8%C%/HV9fAw?.(B */
  464.     write(fd1,&data,4);
  465.  
  466.     
  467.     data = 1024;  /* My preffered data size in bytes*/
  468.     nwords = data>>2;
  469.  
  470.     write(fd1,&nwords,4);
  471.  
  472.     read(fd1,&data,4);
  473.  
  474.     /* $B%A%'%C%/(B */
  475.     if( data != YYMAGIC ){
  476.       close(SYSTEM_GLOBAL_VALUE(fd1));
  477.         return(nil);
  478.     }
  479.     idnumber++;
  480.     
  481.     /* $B%Q%1%C%H%5%$%:(B $B<u?.(B */
  482.     read(SYSTEM_GLOBAL_VALUE(fd1),&data,4);
  483.  
  484.     SYSTEM_GLOBAL_VALUE(fd2) = get_inet_domain(stringof(hostname), idnumber);
  485.  
  486.     SYSTEM_GLOBAL_VALUE(packet_size)=data<<2;
  487.     fprintf(stderr,"Initialised[%d]\n",data<<2);
  488.     fflush(stderr);
  489.     return lisptrue;
  490. }
  491. EUFUN_CLOSE
  492.  
  493. int get_inet_domain(char *host, int no)
  494. {
  495.         int sock;
  496.         struct sockaddr_in addr;
  497.         struct hostent *hp;
  498.         bzero((char *)&addr, sizeof(addr));
  499.         if ((hp = gethostbyname(host)) == (struct hostent *)NULL) {
  500.                 fprintf(stderr, "Unknown host %s\n", host);
  501.                 return -1;
  502.         }
  503.         addr.sin_family = hp->h_addrtype;
  504. #ifdef hlh /* 4.2 BSD socketism */
  505.     bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
  506. #else
  507.         bcopy(*hp->h_addr_list, &addr.sin_addr, hp->h_length);
  508. #endif
  509.         addr.sin_port = (YYPROTO_INET_PORT+no);
  510.         if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
  511.                 perror("socket");
  512.                 return -1;
  513.         }
  514.         if (connect(sock, &addr, sizeof(addr)) < 0) {
  515.                 perror("connect");
  516.                 return -1;
  517.         }
  518.         fprintf(stderr, "Connect on %d/INET\n", sock);
  519.         return sock;
  520. }
  521.  
  522.  
  523. /* 
  524.   Shamelessly nicked from kclient
  525.   */
  526.  cmd_arg yy_init_arg[] = {
  527. { ARG_INT, YYPROTO_MAGIC, NULL, "Magic" },
  528. { ARG_INT, YYPROTO_VERSION, NULL, "Version" },
  529. { ARG_INT, YYPROTO_RELEASE, NULL, "Release" },
  530. { ARG_INT, YYPROTO_SYNC_COUNTER, NULL, "Sync" },
  531. { ARG_INT, YYPROTO_MOUSE_STAY_TIME, NULL, "Mouse" },
  532. { ARG_STR, 0, "prophet:0", "Server" },
  533. { ARG_STR, 0, NULL, "Window Name" },
  534. { ARG_STR, 0, NULL, "Icon Name" },
  535. { -1, 0, NULL, NULL }
  536. } ;
  537.  
  538. reply_entry yy_init_reply[]= {
  539. {ARG_INT,"Connect. Id"},
  540. {ARG_INT,"Version"},
  541. {ARG_INT,"Release"},
  542. {ARG_INT,"Sync"},
  543. {ARG_INT,"Mouse"},
  544. {ARG_INT,"Width"},
  545. {ARG_INT,"Height"},
  546. {ARG_STR,"label"},
  547. {-1,NULL}};
  548.  
  549.  cmd_arg yy_create_arg[] = {
  550. { ARG_INT, 0, NULL, "X Position" },
  551. { ARG_INT, 0, NULL, "Y Position" },
  552. { ARG_INT, YYWINDEFAULTWIDTH, NULL, "Windth" },
  553. { ARG_INT, YYWINDEFAULTHEIGHT, NULL, "Height" },
  554. { ARG_INT, -1, NULL, "Parent" },
  555. { ARG_INT, 1, NULL, "Display" },
  556. { ARG_INT, 1, NULL, "Drawable" },
  557. { -1, 0, NULL, NULL }
  558. } ;
  559.  
  560. reply_entry yy_create_reply[] = {
  561. { ARG_INT, "Territory #"},
  562. { -1, NULL}};
  563.  
  564.  cmd_arg yy_display_arg[] = {
  565. { ARG_INT, -1, NULL, "No" },
  566. { ARG_INT, -1, NULL, "Visible" },
  567. { -1, 0, NULL, NULL }
  568. } ;
  569.  cmd_arg yy_move_arg[] = {
  570. { ARG_INT, -1, NULL, "No" },
  571. { ARG_INT, -1, NULL, "X Position" },
  572. { ARG_INT, -1, NULL, "Y Position" },
  573. { -1, 0, NULL, NULL }
  574. } ;
  575.  cmd_arg yy_resize_arg[] = {
  576. { ARG_INT, -1, NULL, "No" },
  577. { ARG_INT, -1, NULL, "X Position" },
  578. { ARG_INT, -1, NULL, "Y Position" },
  579. { ARG_INT, -1, NULL, "Windth" },
  580. { ARG_INT, -1, NULL, "Height" },
  581. { ARG_INT, -1, NULL, "New X Position" },
  582. { ARG_INT, -1, NULL, "New Y Position" },
  583. { -1, 0, NULL, NULL }
  584. } ;
  585.  cmd_arg yy_destroy_arg[] = {
  586. { ARG_INT, -1, NULL, "No" },
  587. { -1, 0, NULL, NULL }
  588. } ;
  589.  cmd_arg yy_reparent_arg[] = {
  590. { ARG_INT, -1, NULL, "No" },
  591. { ARG_INT, -1, NULL, "Parent" },
  592. { ARG_INT, -1, NULL, "X Position" },
  593. { ARG_INT, -1, NULL, "Y Position" },
  594. { -1, 0, NULL, NULL }
  595. } ;
  596.  cmd_arg yy_raise_arg[] = {
  597. { ARG_INT, -1, NULL, "No" },
  598. { -1, 0, NULL, NULL }
  599. } ;
  600.  cmd_arg yy_lower_arg[] = {
  601. { ARG_INT, -1, NULL, "No" },
  602. { -1, 0, NULL, NULL }
  603. } ;
  604.  
  605.  cmd_arg yy_draw_point_arg[] = {
  606. { ARG_INT, -1, NULL, "No" },
  607. { ARG_INT, -1, NULL, "X Position" },
  608. { ARG_INT, -1, NULL, "Y Position" },
  609. { ARG_INT, 15, NULL, "Operation" },
  610. { ARG_INT, 0, NULL, "Color" },
  611. { -1, 0, NULL, NULL }
  612. } ;
  613.  cmd_arg yy_draw_line_arg[] = {
  614. { ARG_INT, -1, NULL, "No" },
  615. { ARG_INT, -1, NULL, "X1 Position" },
  616. { ARG_INT, -1, NULL, "Y1 Position" },
  617. { ARG_INT, -1, NULL, "X2 Position" },
  618. { ARG_INT, -1, NULL, "Y2 Position" },
  619. { ARG_INT, 1, NULL, "Line Width" },
  620. { ARG_INT, 15, NULL, "Operation" },
  621. { ARG_INT, 0, NULL, "Edge" },
  622. { ARG_INT, 0, NULL, "Color" },
  623. { ARG_INT, 0, NULL, "Dash" },
  624. { -1, 0, NULL, NULL }
  625. } ;
  626.  cmd_arg yy_draw_rectangle_arg[] = {
  627. { ARG_INT, -1, NULL, "No" },
  628. { ARG_INT, -1, NULL, "X Position" },
  629. { ARG_INT, -1, NULL, "Y Position" },
  630. { ARG_INT, -1, NULL, "Width" },
  631. { ARG_INT, -1, NULL, "Height" },
  632. { ARG_INT, 1, NULL, "Line Width" },
  633. { ARG_INT, 15, NULL, "Operation" },
  634. { ARG_INT, 0, NULL, "Color" },
  635. { ARG_INT, 0, NULL, "Dash" },
  636. { -1, 0, NULL, NULL }
  637. } ;
  638.  cmd_arg yy_draw_lines_arg[] = {
  639. { ARG_INT, -1, NULL, "No" },
  640. { ARG_INT, 3, NULL, "Number of Points" },
  641. { ARG_PAIR_LIST, -1, NULL, "Points"},
  642. { ARG_INT, 1, NULL, "Line Width" },
  643. { ARG_INT, 15, NULL, "Operation" },
  644. { ARG_INT, 0, NULL, "Edge" },
  645. { ARG_INT, 0, NULL, "Joint" },
  646. { ARG_INT, 0, NULL, "Color" },
  647. { ARG_INT, 0, NULL, "Dash" },
  648. { -1, 0, NULL, NULL }
  649. } ;
  650.  cmd_arg yy_draw_polygon_arg[] = {
  651. { ARG_INT, -1, NULL, "No" },
  652. { ARG_INT, 3, NULL, "Number of Points" },
  653. { ARG_PAIR_LIST, -1, NULL, "Positions"},
  654. { ARG_INT, 1, NULL, "Line Width" },
  655. { ARG_INT, 15, NULL, "Operation" },
  656. { ARG_INT, 0, NULL, "Joint" },
  657. { ARG_INT, 0, NULL, "Color" },
  658. { ARG_INT, 0, NULL, "Dash" },
  659. { -1, 0, NULL, NULL }
  660. } ;
  661.  cmd_arg yy_draw_circle_arg[] = {
  662. { ARG_INT, -1, NULL, "No" },
  663. { ARG_INT, -1, NULL, "X Position" },
  664. { ARG_INT, -1, NULL, "Y Position" },
  665. { ARG_INT, -1, NULL, "Radious" },
  666. { ARG_INT, 1, NULL, "Line Width" },
  667. { ARG_INT, 15, NULL, "Operation" },
  668. { ARG_INT, 0, NULL, "Color" },
  669. { ARG_INT, 0, NULL, "Dash" },
  670. { -1, 0, NULL, NULL }
  671. } ;
  672.  
  673.  cmd_arg yy_fill_circle_arg[] = {
  674. { ARG_INT, -1, NULL, "No" },
  675. { ARG_INT, -1, NULL, "X Position" },
  676. { ARG_INT, -1, NULL, "Y Position" },
  677. { ARG_INT, -1, NULL, "Radious" },
  678. { ARG_INT, 15, NULL, "Operation" },
  679. { ARG_INT, 0, NULL, "Color" },
  680. { ARG_INT, 0, NULL, "Pattern" },
  681. { -1, 0, NULL, NULL }
  682. } ;
  683.  
  684. cmd_arg yy_draw_fill_rectangle_arg[] = {
  685. { ARG_INT, -1, NULL, "No" },
  686. { ARG_INT, -1, NULL, "X Position" },
  687. { ARG_INT, -1, NULL, "Y Position" },
  688. { ARG_INT, -1, NULL, "Width" },
  689. { ARG_INT, -1, NULL, "Height" },
  690. { ARG_INT, -1, NULL, "Operation"},
  691. { ARG_INT, -1, NULL, "Colour"},
  692. { ARG_INT, -1, NULL, "Fill"},
  693. { -1, -1, NULL, NULL}};
  694.  
  695.  
  696.  cmd_arg yy_draw_arc_arg[] = {
  697. { ARG_INT, -1, NULL, "No" },
  698. { ARG_INT, -1, NULL, "X Position" },
  699. { ARG_INT, -1, NULL, "Y Position" },
  700. { ARG_INT, -1, NULL, "Radious" },
  701. { ARG_INT, -1, NULL, "Angle1" },
  702. { ARG_INT, -1, NULL, "Angle2" },
  703. { ARG_INT, 1, NULL, "Line Width" },
  704. { ARG_INT, 15, NULL, "Operation" },
  705. { ARG_INT, 0, NULL, "Color" },
  706. { ARG_INT, 0, NULL, "Dash" },
  707. { -1, 0, NULL, NULL }
  708. } ;
  709.  cmd_arg yy_load_font_arg[] = {
  710. { ARG_STR, -1, NULL, "Font Name" },
  711. { -1, 0, NULL, NULL }
  712. } ;
  713.  
  714. reply_entry yy_load_font_reply[] = {
  715. { ARG_INT, "Font number"},
  716. { ARG_FONTDATA, "Font data"},
  717. { -1, NULL}};
  718.  
  719.  cmd_arg yy_draw_text_arg[] = {
  720. { ARG_INT, -1, NULL, "No" },
  721. { ARG_INT, -1, NULL, "X Position" },
  722. { ARG_INT, -1, NULL, "Y Position" },
  723. { ARG_INT, 15, NULL, "Operation" },
  724. { ARG_INT, 0, NULL, "Color" },
  725. { ARG_INT, 1, NULL, "Font ID" },
  726. { ARG_STR, 0, "TEST TEST", "Text" },
  727. { -1, 0, NULL, NULL }
  728. } ;
  729.  cmd_arg yy_draw_rtext_arg[] = {
  730. { ARG_INT, -1, NULL, "No" },
  731. { ARG_INT, -1, NULL, "X Position" },
  732. { ARG_INT, -1, NULL, "Y Position" },
  733. { ARG_INT, 15, NULL, "Operation" },
  734. { ARG_INT, 0, NULL, "Color" },
  735. { ARG_INT, 1, NULL, "Font ID" },
  736. { ARG_INT, 1, NULL, "X Scale" },
  737. { ARG_INT, 1, NULL, "Y Scale" },
  738. { ARG_INT, 0, NULL, "Angle" },
  739. { ARG_STR, 0, "TEST TEST", "Text" },
  740. { -1, 0, NULL, NULL }
  741. } ;
  742.  cmd_arg yy_select_territory_arg[] = {
  743. { ARG_INT, -1, NULL, "No" },
  744. { -1, 0, NULL, NULL }
  745. } ;
  746.  cmd_arg yy_set_event_mask_arg[] = {
  747. { ARG_INT, -1, NULL, "No" },
  748. { ARG_INT, 511, NULL, "Mask" },
  749. { -1, 0, NULL, NULL }
  750. } ;
  751.  
  752.  cmd_arg yy_mask_event_arg[] = {
  753. { ARG_INT, 0 , NULL,"Flag"},
  754. { -1, 0, NULL, NULL }
  755. } ;
  756.  
  757.  cmd_arg yy_clear_territory_arg[] = {
  758. { ARG_INT, -1, NULL, "No" },
  759. { ARG_INT, 0, NULL, "Color" },
  760. { -1, 0, NULL, NULL }
  761. } ;
  762.  
  763.  cmd_arg yy_draw_background_arg[] = {
  764. { ARG_INT, -1, NULL, "No" },
  765. { ARG_INT, -1, NULL, "Pattern" },
  766. { -1, 0, NULL, NULL }
  767. } ;
  768.  cmd_arg yy_query_color_arg[] = {
  769. { ARG_INT, -1, NULL, "Red" },
  770. { ARG_INT, -1, NULL, "Green" },
  771. { ARG_INT, -1, NULL, "Blue" },
  772. { -1, 0, NULL, NULL }
  773. } ;
  774.  
  775. reply_entry yy_query_color_reply[] = {
  776. { ARG_INT, "colour number"},
  777. { -1, NULL}};
  778.  
  779. cmd_arg yy_load_image_arg[] = {
  780. { ARG_INT, -1, NULL, "Must be 1 (# pics)"},
  781. { ARG_INT, -1, NULL, "territory"},
  782. { ARG_STR, -1, NULL, "File Name"},
  783. { -1, 0, NULL, NULL}
  784. };
  785.  
  786. cmd_arg yy_operate_bitblt_arg[] = {
  787. { ARG_INT, -1, NULL, "Source Territory number "},
  788. { ARG_INT, -1, NULL, "left-top position X of source"},
  789. { ARG_INT, -1, NULL, "Y"},
  790. { ARG_INT, -1, NULL, "Destination Territory number"},
  791. { ARG_INT, -1, NULL,  "the left-top position X of a area to be filled"},
  792. { ARG_INT, -1, NULL,  "Y    "},
  793. { ARG_INT, -1, NULL,  "Width of the destination area "},
  794. { ARG_INT, -1, NULL,  "Height of the destination area"},
  795. { ARG_INT, -1, NULL,  "bitblt operation"},
  796. { -1 , 0, NULL, NULL}
  797. };
  798.  
  799.  
  800. cmd_arg yy_create_cursor_territory_arg[] = {
  801. { ARG_INT, -1, NULL, "Width" },
  802. { ARG_INT, -1, NULL, "Height" },
  803. { ARG_INT, -1, NULL, "Parent" },
  804. { ARG_INT, -1, NULL, "HotSpot (X)" },
  805. { ARG_INT, -1, NULL, "HotSpot (Y)" },
  806. { ARG_INT, -1, NULL, "BitMap" },
  807. { -1, 0, NULL, NULL }
  808. } ;
  809.  cmd_arg yy_move_cursor_hotspot_arg[] = {
  810. { ARG_INT, -1, NULL, "No" },
  811. { ARG_INT, -1, NULL, "HotSpot (X)" },
  812. { ARG_INT, -1, NULL, "HotSpot (Y)" },
  813. { -1, 0, NULL, NULL }
  814. } ;
  815.  cmd_arg yy_display_cursor_arg[] = {
  816. { ARG_INT, -1, NULL, "No" },
  817. { -1, 0, NULL, NULL }
  818. } ;
  819.  cmd_arg yy_debug_reset_timer_arg[] = {
  820. { -1, 0, NULL, NULL }
  821. } ;
  822.  cmd_arg yy_debug_set_timer_arg[] = {
  823. { ARG_INT, -1, NULL, "CMD No" },
  824. { ARG_INT, -1, NULL, "ON(1), OFF(0)" },
  825. { -1, 0, NULL, NULL }
  826. } ;
  827.  cmd_arg yy_debug_show_table_arg[] = {
  828. { -1, 0, NULL, NULL }
  829. } ;
  830.  cmd_arg yy_debug_list_color_arg[] = {
  831. { ARG_STR, 0, "black", "Color Name" },
  832. { -1, 0, NULL, NULL }
  833. } ;
  834.  cmd_arg yy_debug_list_territory_arg[] = {
  835. { -1, 0, NULL, NULL }
  836. } ;
  837.  
  838. cmd_arg yy_mouse_event[] = {{ -1, 0, NULL, NULL}};
  839.  
  840. reply_entry yy_mouse_event_reply[]= {
  841. {ARG_INT,"Territory"},
  842. {ARG_INT,"input status"},
  843. {ARG_INT,"X"},
  844. {ARG_INT,"Y"},
  845. {-1,NULL}};
  846.  
  847.  
  848. command_entry ClientCmdTable[256] ;
  849.  
  850.  
  851. /* Throwing data to and from the socket */
  852. int send_packet(int sock, yy_packet *pkt)
  853. {
  854.   yy_packet_block *blk;
  855.  
  856.   debug_setfunc("control", "send_packet");
  857.  
  858.   errno=0;
  859.  
  860.   for (blk = &pkt->pktFirstBlock;
  861.        blk != (yy_packet_block *)NULL; blk = blk->pbNextBlock) 
  862.     {
  863.       debug_print(1, "send packet (length=%d)\n", blk->pbLength);
  864.       if (send(sock, blk->pbBuf, blk->pbLength, 0) <= 0)
  865.     {
  866.       perror("send");
  867.       fprintf(stderr,"Sock:%d\n",sock);
  868.       debug_endfunc("send_packet");
  869.       return FALSE;
  870.     }
  871.     }
  872.   debug_endfunc("send_packet");
  873.   return TRUE;
  874. }
  875.  
  876. recv_packet(int sock,yy_packet_system_queue *p_que)
  877. {
  878.   debug_setfunc("control", "recv_packet");
  879.   bzero((char *)p_que, sizeof(*p_que));
  880.   
  881.   while (p_que->sysqRead==NULL)
  882.       recv_one_block(sock, p_que);
  883.   
  884.   debug_endfunc("control", "recv_packet");
  885. }
  886.  
  887. int recv_one_block(int sock, yy_packet_system_queue *que)
  888. {
  889. #ifdef WITH_SYSTEMV_SOCKETS  
  890.   struct pollfd rfds[1];
  891.   int timeout;
  892.   int ret;
  893. #endif  
  894. #ifdef WITH_BSD_SOCKETS
  895.     fd_set rfds;
  896.     struct timeval timeout;
  897.     int ret;
  898. #endif
  899.   debug_setfunc("network", "recv_one_block");
  900.  retry:
  901. #ifdef WITH_SYSTEMV_SOCKETS
  902.   rfds[0].fd=sock;
  903.   rfds[0].events= POLLIN;
  904.   rfds[0].revents= 0;
  905.  
  906.   timeout =  CLIENTTIMEOUT_INUSEC ;
  907.   if ((ret = poll(rfds, 1, timeout)) < 0) 
  908. #endif
  909. #ifdef WITH_BSD_SOCKETS
  910.   FD_ZERO(&rfds);
  911.   FD_SET(sock, &rfds);
  912.   timeout.tv_sec = 0;
  913.   timeout.tv_usec = CLIENTTIMEOUT_INUSEC;
  914.   if ((ret = select(sock+1, &rfds, NULL, NULL, &timeout)) < 0) 
  915. #endif
  916.     {
  917.       perror("select");
  918.       return FALSE;
  919.     }
  920.   if (ret > 0)
  921.     {
  922.       int leng, pleng, body;
  923.       byte *bp = (byte *)xalloc(packet_size+10);
  924.       /* Recv Header Info */
  925.       if ((leng = recv(sock, bp, YYHEADERSIZE, 0)) != YYHEADERSIZE)
  926.     {
  927.       if (leng < 0 && errno == EWOULDBLOCK) 
  928.         {
  929.           debug_print(9, "recv() call would block\n");
  930.           return 0;
  931.         }
  932.       if (leng == 0)
  933.         return FALSE;
  934.  
  935.       return FALSE;
  936.     }    
  937.       debug_print(5, "recv Header Info\n");
  938.       
  939.       /* Get Packet Length */
  940.       bcopy((char *)bp+1, (char *)&pleng+THREE_BYTE_OFFSET, 3);
  941.  
  942.       pleng = ((pleng & 07777) << 2);
  943.       body = pleng - YYHEADERSIZE;
  944.       debug_print(5, "Header said length is %d\n", pleng);
  945.       if ((leng = recv(sock, bp+YYHEADERSIZE, body, 0)) != body) 
  946.     {
  947.       if (leng < 0 && errno == EWOULDBLOCK) 
  948.         {
  949.           debug_print(9, "recv() call would block\n");
  950.           return 0;
  951.         }
  952.       return FALSE;
  953.     }
  954.       /* If we have fixed packet, it will be moved to read queue */
  955.       debug_print(9, " recv one block (LENGTH:%d)\n", pleng);
  956.       put_block_on_recvq(que, bp, pleng);
  957.       debug_endfunc("recv_yy_packet");
  958.       return TRUE;
  959.     }
  960.   debug_endfunc("recv_one_block");
  961.   return 0;            /* time out */
  962. }
  963.  
  964. #define YY_ENTRIES (3)
  965. MODULE Module_YY_low;
  966. LispObject Module_YY_values[YY_ENTRIES];
  967.  
  968. void INIT_YY_low(LispObject *stacktop)
  969. {
  970.   SYSTEM_INITIALISE_GLOBAL(int,fd1,0);
  971.   SYSTEM_INITIALISE_GLOBAL(int,fd2,0);
  972.  
  973.   /* Enter the initialisation */
  974.   init_entry( & ClientCmdTable[YYCOMMAND_INIT],
  975.          "init", yy_init_arg, "initialisation",yy_init_reply);
  976.   init_entry( & ClientCmdTable[YYCOMMAND_CREATE_TERRITORY],
  977.          "create", yy_create_arg, "Command Name" ,yy_create_reply);
  978.   init_entry( & ClientCmdTable[YYCOMMAND_DISPLAY_TERRITORY],
  979.          "display", yy_display_arg, "Command Name" ,NULL);
  980.   init_entry( & ClientCmdTable[YYCOMMAND_MOVE_TERRITORY],
  981.          "move", yy_move_arg, "Command Name" ,NULL);
  982.   init_entry( & ClientCmdTable[YYCOMMAND_RESIZE_TERRITORY],
  983.          "resize", yy_resize_arg, "Command Name" ,NULL);
  984.   init_entry( & ClientCmdTable[YYCOMMAND_DESTROY_TERRITORY],
  985.          "destroy", yy_destroy_arg, "Command Name" ,NULL);
  986.   init_entry( & ClientCmdTable[YYCOMMAND_REPARENT_TERRITORY],
  987.          "reparent", yy_reparent_arg, "Command Name" ,NULL);
  988.   init_entry( & ClientCmdTable[YYCOMMAND_RAISE_TERRITORY],
  989.          "raise", yy_raise_arg, "Command Name" ,NULL);
  990.   init_entry( & ClientCmdTable[YYCOMMAND_LOWER_TERRITORY],
  991.          "lower", yy_lower_arg, "Command Name" ,NULL);
  992.   init_entry( & ClientCmdTable[YYCOMMAND_LOAD_FONT],
  993.          "load-font", yy_load_font_arg, "Load Font",yy_load_font_reply);
  994.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_POINT],
  995.          "draw-point", yy_draw_point_arg, "Draw Point",NULL);
  996.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_LINE],
  997.          "draw-line", yy_draw_line_arg, "Draw Line",NULL);
  998.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_RECTANGLE],
  999.          "draw-rectangle", yy_draw_rectangle_arg, "Draw Rectangle",NULL);
  1000.   init_entry( & ClientCmdTable[YYCOMMAND_FILL_RECTANGLE],
  1001.          "draw-fill-rectangle", yy_draw_fill_rectangle_arg,"Draw rectangle",NULL);
  1002.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_LINES],
  1003.          "draw-lines", yy_draw_lines_arg, "Draw Lines",NULL);
  1004.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_POLYGON],
  1005.          "draw-polygon", yy_draw_polygon_arg, "Draw Polygon",NULL);
  1006.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_CIRCLE],
  1007.          "draw-circle", yy_draw_circle_arg, "Draw Circle",NULL);
  1008.   init_entry( & ClientCmdTable[YYCOMMAND_FILL_CIRCLE],
  1009.          "fill-circle", yy_fill_circle_arg, "Draw Circle",NULL);
  1010.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_ARC],
  1011.          "draw-arc", yy_draw_arc_arg, "Draw Arc",NULL);
  1012.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_TEXT],
  1013.          "draw-text", yy_draw_text_arg, "Draw Text",NULL);
  1014.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_VTEXT],
  1015.          "draw-vtext", yy_draw_text_arg, "Draw Text",NULL);
  1016.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_RTEXT],
  1017.          "draw-rtext", yy_draw_rtext_arg, "Draw Text",NULL);
  1018.   init_entry( & ClientCmdTable[YYCOMMAND_SELECT_TERRITORY],
  1019.          "select-territory", yy_select_territory_arg, "Select Territory",NULL);
  1020.   init_entry( & ClientCmdTable[YYCOMMAND_SET_EVENT_MASK],
  1021.          "set-event-mask", yy_set_event_mask_arg, "Set Event Mask",NULL);
  1022.   init_entry( & ClientCmdTable[YYCOMMAND_MASK_EVENT],
  1023.          "Set event flag",yy_mask_event_arg,"",NULL);
  1024.   init_entry( & ClientCmdTable[YYCOMMAND_CLEAR_TERRITORY],
  1025.          "clear-territory", yy_clear_territory_arg, "Clear Territory",NULL);
  1026.   init_entry( & ClientCmdTable[YYCOMMAND_DRAW_BACKGROUND],
  1027.          "draw-background", yy_draw_background_arg, "Draw Background",NULL);
  1028.   init_entry( & ClientCmdTable[YYCOMMAND_QUERY_COLOR],
  1029.          "query-color", yy_query_color_arg, "Query Color",yy_query_color_reply);
  1030.   init_entry( &ClientCmdTable[YYCOMMAND_LOAD_PICTURE],
  1031.          "Get-Image", yy_load_image_arg, "Load Image", NULL);
  1032.   init_entry( &ClientCmdTable[YYCOMMAND_OPERATE_BITBLT],
  1033.          "OPERATE BITBLT", yy_operate_bitblt_arg, "Bitblt",NULL);
  1034.   init_entry( & ClientCmdTable[YYCOMMAND_CREATE_CURSOR_TERRITORY],
  1035.          "create-cursor", yy_create_cursor_territory_arg, "Create Cursor Territory",NULL);
  1036.   init_entry( & ClientCmdTable[YYCOMMAND_MOVE_CURSOR_HOTSPOT],
  1037.          "move-cursor-hotspot", yy_move_cursor_hotspot_arg, "Move Cursor Hotspot",NULL);
  1038.   init_entry( & ClientCmdTable[YYCOMMAND_DISPLAY_CURSOR],
  1039.          "display-cursor", yy_display_cursor_arg, "Display Cursor",NULL);
  1040.   init_entry( & ClientCmdTable[YYCOMMAND_MOUSE_EVENT],
  1041.          "Mouse Event",yy_mouse_event,"",yy_mouse_event_reply);
  1042.  
  1043.   open_module(stacktop,&Module_YY_low,Module_YY_values,"YY-low",YY_ENTRIES);
  1044.   
  1045.   (void) make_module_function(stacktop,"YY-function",Fn_YY_Function,-2);
  1046.   (void) make_module_function(stacktop,"YY-connect",Fn_setup_server,2);
  1047.   (void) make_module_function(stacktop,"YY-next-event",Fn_get_next_event,1);
  1048.   close_module();
  1049. }
  1050.  
  1051. init_entry(command_entry *ce,char *name,cmd_arg *ca,char *comment, reply_entry *re)
  1052. {
  1053.   ce->cmdLabel=name;
  1054.   ce->cmdArgs=ca;
  1055.   ce->cmdComment=comment;
  1056.   ce->ceReplyData=re;
  1057. }
  1058.